package oop.data;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import oop.db.Database;

public class User extends SimpleUser {

	private String fullname;
	private String password;
	private String email;
	private String group;
	private boolean logedIn;
	private boolean blocked;
	private String warningMessage;
	
	public static List<User> fetchById(int id) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			conn = Database.get().getConnection();
			stmt = conn.createStatement();
			String sql = "SELECT user_name, user_fullname, user_pass, user_email, " +
					"user_group, user_avatar, user_warning, user_blocked, user_register_date " 
					+ "FROM  tblUser "
					+ "WHERE user_id = " + id;
			rs = stmt.executeQuery(sql);
			
			List<User> users = new ArrayList<User>();
			if (rs.next()) {
				users.add(new User(id, rs.getString(1), rs.getString(2), rs
						.getString(3), rs.getString(4), rs.getString(5)));
			}
			return users;
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}

	public static List<User> fetchByUsername(String name) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			conn = Database.get().getConnection();
			stmt = conn.createStatement();
			String sql = "SELECT user_id, user_fullname, user_password, user_email, " +
					"user_group, user_avatar, user_warning, user_blocked, user_register_date " 
					+ "FROM  `tblTest` "
					+ "WHERE name = " + name;
			rs = stmt.executeQuery(sql);
			
			List<User> users = new ArrayList<User>();
			if (rs.next()) {
				users.add(new User(rs.getInt(1), name, rs.getString(2), rs
						.getString(3), rs.getString(4), rs.getString(5)));
			}
			return users;
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}
	
	public static List<User> fetch(int start, int length) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			conn = Database.get().getConnection();
			stmt = conn.createStatement();
			String sql = "SELECT user_name, user_fullname, user_password, user_email, " +
					"user_group, user_avatar, user_warning, user_blocked, user_register_date " +
					"user_id" 
					+ "FROM  `tblTest` "
					+ "LIMIT " + start + ", " + length;
			rs = stmt.executeQuery(sql);
			
			List<User> users = new ArrayList<User>();
			if (rs.next()) {
				users.add(new User(rs.getInt(6), rs.getString(1), rs.getString(2),
						rs.getString(3), rs.getString(4), rs.getString(5)));
			}
			return users;
		} finally {
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			if (conn != null) conn.close();
		}
	}
	
	public User(int id, String name, String fullname, String password, String email,
			String group) {
		super();
		this.id = id;
		this.name = name;
		this.fullname = fullname;
		this.password = password;
		this.email = email;
		this.group = group;
	}

	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public boolean isLogedIn() {
		return logedIn;
	}

	public void setLogedIn(boolean logedIn) {
		this.logedIn = logedIn;
	}

	public boolean isBlocked() {
		return blocked;
	}

	public void setBlocked(boolean blocked) {
		this.blocked = blocked;
	}

	public String getWarningMessage() {
		return warningMessage;
	}

	public void setWarningMessage(String warningMessage) {
		this.warningMessage = warningMessage;
	}

}
